package com.yc.sakuralxm.uestcscanning.tools


import com.alibaba.fastjson.JSON

import java.util.ArrayList
import java.util.HashMap

/**
 * Created by Administrator on 2016/5/10.
 */
object ListUtil {

    fun <T> empty(): ArrayList<T> {
        return ArrayList()
    }

    fun <K, V> emptyHashMap(): HashMap<K, V> {
        return HashMap()
    }

    fun toJson(o: Any?): String? {
        return if (o == null) {
            null
        } else JSON.toJSONString(o)

    }

    /**
     * 根据数组得到arrayList列表
     *
     * @param array
     * @return
     */
    @SafeVarargs
    fun <T> asList(vararg array: T): ArrayList<T> {
        val data = empty<T>()
        if (array != null) {
            for (t in array) {
                data.add(t)
            }
        }
        return data
    }

    fun <T> subList(list: List<T>?, i: Int, j: Int): List<T>? {
        if (list == null) {
            return null
        }
        val list2 = empty<T>()
        for (j2 in list.indices) {
            if (j2 >= i && j2 < j) {
                list2.add(list[j2])
            }
        }
        return list2
    }

    @SafeVarargs
    fun <T> addToList(list: ArrayList<T>?, vararg subitem: T): Int {
        var count = 0
        if (list != null && subitem != null) {
            for (t in subitem) {
                val index = list.indexOf(t)
                if (index > -1) {
                    list.removeAt(index)
                    list.add(index, t)
                } else {
                    list.add(t)
                    count++
                }
            }
        }
        return count
    }

    fun <T> addToList(list: MutableList<T>?, items: List<T>?): Int {
        var count = 0
        if (list != null && items != null) {
            for (t in items) {
                val index = list.indexOf(t)
                if (index > -1) {
                    list.removeAt(index)
                    list.add(index, t)
                } else {
                    list.add(t)
                    count++
                }
            }
        }
        return count
    }

    /**
     * 添加指定数量的item到List
     *
     * @param list
     * @param subitem
     * @param addCount
     * @return
     */
    @SafeVarargs
    fun <T> addToList(list: MutableList<T>?, addCount: Int, vararg subitem: T): Int {
        var count = 0
        if (list != null && subitem != null) {
            for (t in subitem) {
                val index = list.indexOf(t)
                if (index > -1) {
                    list.removeAt(index)
                    list.add(index, t)
                } else {
                    list.add(t)
                    count++
                    if (count >= addCount) {
                        break
                    }
                }
            }
        }
        return count
    }

    @SafeVarargs
    fun <T> removeFromList(list: MutableList<T>?, vararg subitem: T): Int {
        var count = 0
        if (list != null && subitem != null) {
            for (t in subitem) {
                val index = list.indexOf(t)
                if (index > -1) {
                    list.removeAt(index)
                    count++
                }
            }
        }
        return count
    }

    fun isEmpty(map: Map<String, Any>?): Boolean {
        return map == null || map.isEmpty()
    }

    fun isEmpty(list: List<*>?): Boolean {
        return list == null || list.isEmpty()
    }

    fun isListHasIndex(list: List<*>?, index: Int): Boolean {
        return list != null && list.size > index && index >= 0
    }

    fun size(list: List<*>): Int {
        return if (isEmpty(list)) 0 else list.size
    }

    fun <T> size(items: Array<T>?): Int {
        return items?.size ?: 0
    }

    /**
     * 解析为list
     */
    //    public static <T> ArrayList<T> parseArray(String data, Class<T> t) {
    //        ArrayList<T> result = null;
    //        if (data == null || data.equals("[]")) {
    //            result = empty();
    //        } else {
    //            Type type = new TypeToken<ArrayList<T>>() {
    //            }.getType();
    //            result = GSON.fromJson(data, type);
    //        }
    //        return result;
    //    }

    /**
     *
     * 解析为list
     */
    fun <T> parseArray(data: String?, t: Class<T>): List<T>? {
        var result: List<T>? = null
        if (data == null || data == "[]") {
            result = empty()
        } else {
            try {
                result = JSON.parseArray(data, t)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        return result
    }

    /**
     *
     * 解析为对象
     */
    fun <T> parse(data: String?, t: Class<T>): T? {
        var result: T? = null
        if (data == null || data == "{}" || data == "null") {
        } else {
            // result = new Gson().fromJson(data,t);
            if (t == String::class.java) {
                return data as T?
            }
            try {
                result = JSON.parseObject(data, t)
            } catch (e: com.alibaba.fastjson.JSONException) {
                e.printStackTrace()
            }

        }
        return result
    }

    fun <T> copyList(list: MutableList<T>, minSize: Int) {
        if (isEmpty(list)) {
            return
        }
        while (list.size < minSize) {
            list.addAll(list)
        }
    }
}
